package cn.mygweb.admin.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mygweb.admin.modules.sys.pojo.Menu;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;
import cn.mygweb.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;

/** 
* <p>Title: SysMenuController.java</p>
* <p>Description: 系统菜单</p>  
* @author gxb 
* @date 2019年6月2日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/sys/menu")
@Slf4j
public class MenuController extends BaseController{
	
	//redis缓存的键名称
	private String menuListKeyName;
	
	private String listKeyNameSuffix = ":admin:sys:menu:list";
	
	private String menuSidebarKeyName;
	
	private String sidebarKeyNameSuffix = ":admin:sys:menu:sidebarMenuTree";
	
	/**
	 * <p>Title: menuListLayer</p>
	 * <p>Description: 系统菜单管理</p>
	 * @param model
	 * @return
	 */
	@RequestMapping("/menu")
	public String menu(ModelMap model) {
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			menuListKeyName = super.getCurrentUserId() + listKeyNameSuffix;
			if(super.isNullOrBlank(menuListKeyName)) {
				menuList = menuService.queryMenuList(new Menu());
				if(menuList != null)
					super.redisSet(menuListKeyName, JsonUtils.objectToJson(menuList));
			}
			String jsonList = super.redisGet(menuListKeyName);
			menuList = JsonUtils.jsonToList(jsonList, Menu.class);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("路径【/admin/sys/menu/menu】使用redis发生异常，开始从数据库中查询信息");
			menuList = menuService.queryMenuList(new Menu());
		}
		model.addAttribute("menuList", menuList);
		return "admin/modules/sys/menu";
	}
	
	/**
	 * <p>Title: save</p>
	 * <p>Description: 添加菜单</p>
	 * @param menu
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public JsonResult save(Menu menu) {
		try {
			if(menu != null) {
				menu.setId(sid.nextShort());
				menu.setPerm(menu.getPerm().replace("：", ":"));
				menuService.save(menu);
				super.delRedisCacheByKeys(menuListKeyName,menuSidebarKeyName);
			}
			return JsonResult.ok("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("添加失败");
		}
	}
	
	/**
	 * <p>Title: delete</p>
	 * <p>Description: 删除菜单信息</p>
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(String id) {
		try {
			menuService.deleteById(id);
			super.delRedisCacheByKeys(menuListKeyName,menuSidebarKeyName);
			return JsonResult.ok("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("删除失败");
		}
	}
	
	/**
	 * <p>Title: update</p>
	 * <p>Description: 修改菜单信息</p>
	 * @param menu
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public JsonResult update(Menu menu) {
		try {
			menu.setPerm(menu.getPerm().replace("：", ":"));
			menuService.update(menu);
			super.delRedisCacheByKeys(menuListKeyName,menuSidebarKeyName);
			return JsonResult.ok("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("删除失败");
		}
	}
	
	/**
	 * <p>Title: ajaxMenuList</p>
	 * <p>Description: 异步加载菜单信息</p>
	 * @return
	 */
	@RequestMapping("/ajaxMenuList")
	@ResponseBody
	public JsonResult ajaxMenuList() {
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			menuListKeyName = super.getCurrentUserId() + listKeyNameSuffix;
			if(super.isNullOrBlank(menuListKeyName)) {
				menuList = menuService.queryMenuList(new Menu());
				if(menuList != null)
					super.redisSet(menuListKeyName, JsonUtils.objectToJson(menuList));
			}
			String jsonList = super.redisGet(menuListKeyName);
			menuList = JsonUtils.jsonToList(jsonList, Menu.class);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("路径【/admin/sys/menu/ajaxMenuList】使用redis查询发生异常，开始从数据库中查询信息");
			menuList = menuService.queryMenuList(new Menu());
		}
		if(menuList != null)
			return JsonResult.ok(menuList);
		else
			return JsonResult.errorMsg("暂无查询结果");
	}
	
	/**
	 * <p>Title: ajaxRoleMenus</p>
	 * <p>Description: 异步获取角色对应的菜单信息</p>
	 * @return
	 */
	@RequestMapping("/ajaxRoleMenus")
	@ResponseBody
	public JsonResult ajaxRoleMenus(String roleId) {
		List<Menu> menuList = menuService.getRoleMenus(roleId);
		if((1 == menuList.size() && null == menuList.get(0)) || 0 == menuList.size())
			return JsonResult.errorMsg("暂无查询结果");
		else
			return JsonResult.ok(menuList);
	}
	
	/**
	 * <p>Title: ajaxMenuTree</p>
	 * <p>Description: 异步查询菜单中全部或者指定角色的id,pid,name信息，为树状结构使用</p>
	 * @return
	 */
	@RequestMapping("/menuTree")
	@ResponseBody
	public JsonResult ajaxMenuTree() {
		List<Menu> menuList = menuService.getMenuTree(new Menu());
		if(menuList != null && !menuList.isEmpty())
			return JsonResult.ok(menuList);
		else
			return JsonResult.errorMsg("暂无查询结果");
	}
	
	/**
	 * <p>Title: sidebarMenuTree</p>
	 * <p>Description: 根据用户的权限获取用户所拥有的侧边栏信息，
	 * 				其结果为包含子菜单List集合的Menu对象集合</p>
	 * @return
	 */
	@RequestMapping("/sidebarMenuTree")
	@ResponseBody
	public JsonResult sidebarMenuTree() {
		String userId = super.getCurrentUserId();
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			menuSidebarKeyName = super.getCurrentUserId() + sidebarKeyNameSuffix;
			if(super.isNullOrBlank(menuSidebarKeyName)) {
				menuList = menuService.getSidebarMenuTree(userId, new Menu());
				if(menuList != null)
					super.redisSet(menuSidebarKeyName, JsonUtils.objectToJson(menuList));
			}
			menuList = JsonUtils.jsonToList(super.redisGet(menuSidebarKeyName), Menu.class);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("路径【/admin/sys/menu/sidebarMenuTree】使用redis发生异常，开始从数据库中查询信息");
			menuList = menuService.getSidebarMenuTree(userId, new Menu());
		}
		if(menuList != null && !menuList.isEmpty())
			return JsonResult.ok(menuList);
		else
			return JsonResult.errorMsg("暂无查询结果");
	}
		
}
